home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Deutsche Edition 1
/
Deutsche Edition 1.iso
/
amok
/
amok_lha
/
amok56.lha
/
TurboFiles_V2.0
/
TFD.mod
< prev
next >
Wrap
Text File
|
1993-08-15
|
14KB
|
510 lines
(**********************************************************************
:Program. TFD.mod
:Contents. Some File-Operations using (Turbo)Files
:Author. Stefan Salewski
:Copyright. FD
:Language. Oberon
:Translator. Oberon Compiler V2.0
:History. V1.1 12-06-91
:Address. Stolper Weg 3, D-2160 Stade
:Imports. (Turbo)Files
**********************************************************************)
MODULE TFD;
IMPORT
TF:TurboFiles,
Break,
NoGuru,
io,
SYSTEM,
Arguments,
Strings,
ASCII,
Conversions,
Intuition;
CONST
prt='PRT:';
fnf='File not found';
BufSize=512;
StringLength=80;
TYPE
TwoChars=ARRAY 3 OF CHAR;
FileName=ARRAY StringLength+1 OF CHAR;
BigString=ARRAY 256 OF CHAR;
VAR
file:TF.File;
i,act:LONGINT;
numArgs:INTEGER;
c:CHAR;
arg1:FileName;
arg2:TwoChars;
arg3:BigString;
short:TwoChars;
len1,len2,len3:INTEGER;
PROCEDURE MinLongInt(i,j:LONGINT):LONGINT;
BEGIN
IF i<j THEN RETURN i ELSE RETURN j END;
END MinLongInt;
PROCEDURE WriteStr(s:ARRAY OF CHAR);
(* $CopyArrays- *)
BEGIN
io.WriteString(s);
io.WriteLn;
END WriteStr;
PROCEDURE GetArg3(text:ARRAY OF CHAR);
(* $CopyArrays- *)
BEGIN
IF arg3[0]=0X THEN
io.WriteString(text);
io.ReadString(arg3);
len3:=Strings.Length(arg3);
io.WriteLn;
END;
END GetArg3;
PROCEDURE Point(VAR c:CHAR;ok:CHAR);
BEGIN
IF (c#ok) AND ((c<=CHR(31)) OR ((c>=CHR(128)) AND (c<=CHR(159)))) THEN
c:='.'
END
END Point;
PROCEDURE Usage;
BEGIN
Arguments.GetArg(0,arg3);
io.WriteLn;
io.WriteString(arg3);
WriteStr(': A simpel program to show the use of the module (Turbo)Files');
io.WriteLn;
WriteStr('(c) 1989 by Stefan Salewski');
WriteStr(' Stolper Weg 3');
WriteStr(' D-2160 Stade');
io.WriteLn;
WriteStr('Usage:');
io.WriteString(arg3);
WriteStr(" FileName (C|D|E|H|I|L|N|P|S|T) [String]");
WriteStr("The letter determines the Action:");
WriteStr("C: Codes the File 'FileName' with 'String'");
WriteStr("D: Decodes the File 'FileName' with 'String'");
WriteStr("E: Compares the file 'FileName' with file 'String'");
WriteStr("H: Types the file 'FileName' as HexDump");
WriteStr("I: Transforms the File 'FileName' to M2-INLINE-Code");
WriteStr("L: Prints the file 'FileName' with Linenumbers");
WriteStr("N: Types the file 'FileName' with LineNumbers");
WriteStr("P: Prints the file 'FileName' with header and pagenumbers");
WriteStr("S: Search in the File 'FileName' for 'String'");
WriteStr("T: Types the File 'FileName'");
END Usage;
PROCEDURE Print(numbers:BOOLEAN);
CONST
Top=4;
Header=2;
Bottom=4;
init='\ec\e#1';
underlineOn= '\[4m';
underlineOff='\[24m';
VAR
printer:TF.File;
str1:ARRAY 7 OF CHAR;
i,line,row,page,pageSize,paperWidth,paperLength:INTEGER;
fileSize:LONGINT;
PROCEDURE GetPaper(VAR len,width:INTEGER);
VAR
p:Intuition.Preferences;
BEGIN
Intuition.GetPrefs(p,SYSTEM.SIZE(Intuition.Preferences));
len:=p.paperLength;
width:=p.printRightMargin-p.printLeftMargin;
END GetPaper;
PROCEDURE NewLine(lines:INTEGER);
CONST
crlf='\eE';
VAR
i:INTEGER;
BEGIN
i:=0;
WHILE i<lines DO
IF TF.WriteString(printer,crlf) THEN END;
INC(i);
END
END NewLine;
BEGIN
GetPaper(paperLength,paperWidth);
line:=0;
row:=0;
page:=0;
c:=ASCII.eol;
pageSize:=paperLength-Top-Header-Bottom;
io.WriteString('Printing the File: ');
WriteStr(arg1);
io.WriteLn;
IF TF.Open(file,arg1,BufSize,TF.oldFile) THEN
IF TF.Open(printer,prt,BufSize,TF.newFile) THEN
fileSize:=TF.Size(file);
IF TF.WriteString(printer,init) THEN END;
WHILE (file.res=TF.done) AND (printer.res=TF.done) DO
IF (c=ASCII.eol) OR (row MOD paperWidth=0) THEN
IF c#ASCII.eol THEN
IF TF.WriteChar(printer,c) THEN END;
END;
IF ((line MOD pageSize)=0) AND (TF.GetPos(file)<fileSize) THEN
IF line>0 THEN
NewLine(Bottom+1);
END;
INC(page);
NewLine(Top);
IF TF.WriteString(printer,underlineOn) THEN END;
IF TF.WriteString(printer,'Page ') THEN END;
IF Conversions.IntToStr(page,str1,10,4,' ') THEN END;
IF TF.WriteString(printer,str1) THEN END;
IF TF.WriteString(printer,' File: ') THEN END;
IF TF.WriteString(printer,arg1) THEN END;
IF TF.WriteString(printer,underlineOff) THEN END;
NewLine(Header);
ELSE
NewLine(1)
END;
INC(line);
IF numbers AND (TF.GetPos(file)<fileSize) THEN
IF Conversions.IntToStr(line,str1,10,5,' ') THEN END;
IF TF.WriteString(printer,str1) THEN END;
IF TF.WriteString(printer,' ') THEN END;
row:=5+2;
ELSE
row:=0;
END;
ELSE
IF TF.WriteChar(printer,c) THEN END;
END;
INC(row);
IF TF.ReadChar(file,c) THEN END;
END;
i:=line MOD pageSize;
IF i>1 THEN
NewLine(paperLength-Top-Header-Bottom-i+1)
END;
NewLine(Bottom);
WriteStr('Ready');
IF TF.Close(printer) THEN END;
ELSE
WriteStr('Cannot open Printer!');
END;
IF TF.Close(file) THEN END;
ELSE
WriteStr(fnf);
END;
END Print;
PROCEDURE Type(numbers:BOOLEAN);
VAR
i,line:INTEGER;
BEGIN
io.WriteString('Typing the File: ');
WriteStr(arg1);
io.WriteLn;
IF TF.Open(file,arg1,BufSize,TF.oldFile) THEN
line:=0;
WHILE file.res=TF.done DO
i:=TF.ReadString(file,arg3);
IF ((i>0) OR (file.res=TF.done)) THEN
IF numbers THEN
INC(line);
io.WriteInt(line,4);
io.Write(' ');
END;
io.WriteString(arg3);
io.WriteLn;
END;
END;
IF TF.Close(file) THEN END;
ELSE
WriteStr(fnf);
END;
END Type;
PROCEDURE TypeHex;
VAR
line:INTEGER;
str2: ARRAY 5 OF CHAR;
BEGIN
io.WriteString('Hexdump of: ');
WriteStr(arg1);
io.WriteLn;
IF TF.Open(file,arg1,BufSize,TF.oldFile) THEN
line:=0;
WHILE file.res=TF.done DO
act:=TF.ReadBytes(file,SYSTEM.ADR(arg3),16);
IF act>0 THEN
io.WriteHex(line,4);
io.Write(':');
i:=0;
WHILE i<16 DO
IF (SYSTEM.VAL(LONGSET,i)*LONGSET{0..1})=LONGSET{} THEN
io.Write(' ');
END;
IF i<act THEN
IF Conversions.IntToStr(SYSTEM.VAL(SHORTINT,arg3[i]),
str2,16,2,'0') THEN END;
io.WriteString(str2)
ELSE
io.WriteString(' ')
END;
INC(i);
END;
io.WriteString(' ');
i:=0;
WHILE i<16 DO
Point(arg3[i],'.');
INC(i);
END;
arg3[act]:=0X;
WriteStr(arg3);
INC(line,16);
END;
END;
IF TF.Close(file) THEN END;
ELSE
WriteStr(fnf);
END;
END TypeHex;
PROCEDURE CodeIt(decode:BOOLEAN);
BEGIN
GetArg3('CodeWord: ');
IF TF.Code(arg1,arg3,decode) THEN
io.WriteString('File ');
io.WriteString(arg1);
io.Write(' ');
IF decode THEN
io.WriteString('de')
END;
io.WriteString('coded with CodeWord: ');
WriteStr(arg3)
ELSE
WriteStr(fnf);
END;
END CodeIt;
PROCEDURE Equal;
VAR
file2:TF.File;
s:ARRAY 7 OF CHAR;
act1,act2:LONGINT;
BEGIN
GetArg3('File to Compare with: ');
IF TF.Open(file,arg1,BufSize,TF.oldFile) THEN
IF TF.Open(file2,arg3,BufSize,TF.oldFile) THEN
s:=' | ';
REPEAT
act1:=TF.ReadBytes(file,SYSTEM.ADR(s[0]),1);
act2:=TF.ReadBytes(file2,SYSTEM.ADR(s[4]),1);
UNTIL (file.res#TF.done) OR (file2.res#TF.done) OR (s[0]#s[4]);
IF (act1=0) AND (act2=0) THEN
WriteStr('The Files are identical!')
ELSIF act1=0 THEN
io.WriteString('End of File ');
io.WriteString(arg1);
WriteStr(' reached!')
ELSIF act2=0 THEN
io.WriteString('End of File ');
io.WriteString(arg3);
WriteStr(' reached!')
ELSE
io.WriteString('Differences at position ');
io.WriteInt(TF.GetPos(file),6);
WriteStr(' detected:');
i:=0;
WHILE (i<10) AND (file.res=TF.done) AND (file2.res=TF.done) DO
INC(i);
Point(s[0],'.');
Point(s[4],'.');
WriteStr(s);
act1:=TF.ReadBytes(file,SYSTEM.ADR(s[0]),1);
act2:=TF.ReadBytes(file2,SYSTEM.ADR(s[4]),1);
END;
END;
IF TF.Close(file2) THEN END;
ELSE
io.WriteString('File ');
io.WriteString(arg3);
WriteStr(' not found');
END;
IF TF.Close(file) THEN END;
ELSE
io.WriteString('File ');
io.WriteString(arg1);
WriteStr(' not found');
END;
END Equal;
PROCEDURE Find;
CONST
underlineColorOn='\[4m\[33m';
normalCharSet='\[0m';
VAR
pos:LONGINT;
BEGIN
GetArg3('String to search: ');
IF (len3>0) AND (TF.Open(file,arg1,BufSize,TF.oldFile)) THEN
REPEAT
pos:=TF.Search(file,arg3,len3);
IF pos=-1 THEN
WriteStr('String not found');
short[0]:='N';
ELSE
io.WriteString('String found at position: ');
io.WriteInt(pos,5);
i:=MinLongInt(pos,180);
IF TF.SetPos(file,-i,TF.current) THEN END;
c:=0X; (* #ASCII.eol *)
WHILE (i>0) AND (c#ASCII.eol) DO
act:=TF.ReadBytes(file,SYSTEM.ADR(c),1);
DEC(i);
END;
IF i=0 THEN
IF TF.SetPos(file,-MinLongInt(pos,180),TF.current) THEN END;
END;
io.WriteLn;
io.WriteLn;
WHILE (TF.GetPos(file)<pos) AND TF.ReadChar(file,c) DO
Point(c,ASCII.eol);
io.Write(c);
END;
io.WriteString(underlineColorOn);
i:=0;
pos:=0;
WHILE TF.ReadChar(file,c) AND (pos<4) AND (i<180) DO
IF c=ASCII.eol THEN
INC(pos)
END;
Point(c,ASCII.eol);
io.Write(c);
INC(i);
IF i=len3 THEN
io.WriteString(normalCharSet);
END;
END;
file.res:=TF.done;
IF TF.SetPos(file,len3-i,TF.current) THEN END;
io.WriteLn;
io.WriteString('Search for next occurence? Y-J/N : ');
io.ReadString(short);
io.WriteLn;
END;
UNTIL (CAP(short[0])#'Y') AND (CAP(short[0])#'J');
IF TF.Close(file) THEN END;
ELSE
WriteStr(fnf);
END;
END Find;
PROCEDURE Inline;
VAR
err:BOOLEAN;
destFile:TF.File;
twoByte:ARRAY 2 OF SHORTINT;
buf:ARRAY 13 OF CHAR;
act,i:LONGINT;
l:INTEGER;
BEGIN
GetArg3('Name of Destination File: ');
IF len3=0 THEN
WriteStr('No Destination File');
RETURN
END;
IF TF.Open(file,arg1,BufSize,TF.oldFile) THEN
IF TF.Open(destFile,arg3,BufSize,TF.newFile) THEN
i:=0;
buf:=' INLINE(';
IF TF.WriteBytes(destFile,SYSTEM.ADR(buf),11) THEN END;
LOOP
IF (i MOD 16)=0 THEN
buf:=' ';
buf[0]:=ASCII.eol;
IF TF.WriteBytes(destFile,SYSTEM.ADR(buf),7) THEN END;
END;
act:=TF.ReadBytes(file,SYSTEM.ADR(twoByte),2);
IF act=2 THEN
l:=SYSTEM.VAL(INTEGER,twoByte)
ELSIF act=1 THEN
l:=twoByte[0]
ELSE
EXIT
END;
INC(i,2);
err:=Conversions.IntToStr(l,buf,16,5,'0');
buf[5]:='H';
buf[6]:=',';
IF TF.WriteBytes(destFile,SYSTEM.ADR(buf),7) THEN END;
END;
buf[0]:=')';
buf[1]:=';';
buf[2]:=ASCII.eol;
IF TF.SetPos(destFile,-1,TF.current) THEN END;
IF TF.WriteBytes(destFile,SYSTEM.ADR(buf),3) THEN END;
io.WriteInt(i,6);
WriteStr(' Bytes Written.');
IF TF.Close(destFile) THEN END;
ELSE
WriteStr('Cannot open Destination File')
END;
IF TF.Close(file) THEN END;
ELSE
WriteStr(fnf)
END;
END Inline;
BEGIN
numArgs:=Arguments.NumArgs();
Arguments.GetArg(1,arg1);
Arguments.GetArg(2,arg2);
Arguments.GetArg(3,arg3);
IF arg1[0]='?' THEN
Usage
ELSE
IF numArgs=0 THEN
WriteStr("Usage: FileName 'x' [String]");
io.WriteString('FileName: ');
io.ReadString(arg1);
END;
IF numArgs<2 THEN
io.WriteString("Command: ");
io.ReadString(arg2);
END;
io.WriteLn;
len1:=Strings.Length(arg1);
len2:=Strings.Length(arg2);
len3:=Strings.Length(arg3);
IF (len2=1) AND (len1>0) THEN
CASE CAP(arg2[0]) OF
|'T':Type(FALSE)
|'S':Find
|'C':CodeIt(FALSE)
|'D':CodeIt(TRUE)
|'H':TypeHex
|'N':Type(TRUE)
|'P':Print(FALSE);
|'L':Print(TRUE);
|'I':Inline
|'E':Equal
|ELSE
Usage;
END;
ELSE
Usage
END;
END;
io.WriteLn;
END TFD.